//
//  XQIdentityPhotoView.m
//  JoinPeso
//
//  Created by 小琦 on 2024/11/23.
//

#import "XQIdentityPhotoView.h"
#import "XQIdentityPhotoView.h"

@interface XQIdentityPhotoView()
@property (nonatomic,strong) UILabel *titleLb;
@property (nonatomic,strong) UIImageView *bgImageView;
@property (nonatomic,strong) UIImageView *photoImageView;
@property (nonatomic,strong) UIButton *upLoadButton;
@end

@implementation XQIdentityPhotoView

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self makeUI];
        [self makeConstraints];
    }
    return self;
}

- (void)makeUI {
    [self addSubview:self.titleLb];
    [self addSubview:self.bgImageView];
    [self.bgImageView addSubview:self.photoImageView];
    [self addSubview:self.upLoadButton];
}

- (void)makeConstraints {
    [self.titleLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@0);
        make.left.equalTo(@12);
        make.height.mas_equalTo(22);
    }];
    [self.bgImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLb.mas_bottom).offset(10);
        make.left.equalTo(@12);
        make.right.equalTo(@-12);
        make.height.mas_equalTo(150);
    }];
    [self.photoImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        (void)make.centerX;
        make.top.equalTo(@15);
        make.width.mas_equalTo(179);
        make.bottom.equalTo(@-22);
    }];
    [self.upLoadButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.bgImageView.mas_bottom).offset(10);
        (void)make.centerX;
        make.width.mas_equalTo(134);
        make.height.mas_equalTo(45);
        make.bottom.equalTo(@0);
    }];
}

#pragma mark - setter

- (void)setTitle:(NSString *)title {
    self.titleLb.text = title;
}

- (void)setImage:(UIImage *)image {
    self.photoImageView.image = image;
}

- (void)setPhotoImageName:(NSString *)photoImageName {
    [self.photoImageView sd_setImageWithURL:[NSURL URLWithString:photoImageName] placeholderImage:[UIImage imageNamed:self.defaultImageName]];
}

- (void)setIsCompleted:(BOOL)isCompleted {
    _isCompleted = isCompleted;
    [self.upLoadButton setTitle:isCompleted ? @"Completed" : @"Upload" forState:UIControlStateNormal];
}

#pragma mark - private

- (void)uoloadClick {
    if (self.isCompleted) return;
    if (self.upLoadButton) {
        self.uploadHandle();
    }
}

#pragma mark - lazy

- (UILabel *)titleLb {
    if (!_titleLb) {
        _titleLb = [[UILabel alloc] init];
        _titleLb.textColor = kBlackTextColor;
        _titleLb.font = [UIFont boldSystemFontOfSize:16];
    }
    return _titleLb;
}

- (UIImageView *)bgImageView {
    if (!_bgImageView) {
        _bgImageView = [[UIImageView alloc] init];
        _bgImageView.image = [UIImage imageNamed:@"photo_cover_bg"];
        [_bgImageView setUserInteractionEnabled:YES];
        [_bgImageView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(uoloadClick)]];
    }
    return _bgImageView;
}

- (UIImageView *)photoImageView {
    if (!_photoImageView) {
        _photoImageView = [[UIImageView alloc] init];
        _photoImageView.layer.cornerRadius = 15;
        _photoImageView.layer.masksToBounds = YES;
    }
    return _photoImageView;
}

- (UIButton *)upLoadButton {
    if (!_upLoadButton) {
        _upLoadButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_upLoadButton setTitle:@"Upload" forState:UIControlStateNormal];
        _upLoadButton.backgroundColor = kOrangeColor;
        [_upLoadButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _upLoadButton.titleLabel.font = [UIFont boldSystemFontOfSize:20];
        _upLoadButton.layer.cornerRadius = 22.5;
        [_upLoadButton addTarget:self action:@selector(uoloadClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _upLoadButton;
}



@end
